package com.bjsxt.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.domain.TradeArea;
import com.bjsxt.dto.CoinDto;
import com.bjsxt.feign.CoinServiceFeign;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.domain.Market;
import com.bjsxt.mapper.MarketMapper;
import com.bjsxt.service.MarketService;
import org.springframework.util.CollectionUtils;

/**
 * @Description:  ${description}
 * @ClassName:      MarketServiceImpl
 * @Author:  chenyu
 * @Date:    2025/3/19 14:47
 */

@Slf4j
@Service
public class MarketServiceImpl extends ServiceImpl<MarketMapper, Market> implements MarketService{

    @Autowired
    private CoinServiceFeign coinServiceFeign;

    @Override
    public Page<Market> findByPage(Page<Market> page, Long tradeAreaId, Byte status) {
        return page(page,
                new LambdaQueryWrapper<Market>()
                        .eq(tradeAreaId!=null, Market::getTradeAreaId, tradeAreaId) // 名称的查询
                        .eq(status!=null, Market::getStatus, status)  // 标题的查询
        );
    }

    @Override
    public boolean save(Market entity) {
        log.info("开始新增市场数据{}", JSON.toJSONString(entity));
        @NotBlank Long sellCoinId = entity.getSellCoinId(); //基础货币
        @NotBlank Long buyCoinId = entity.getBuyCoinId(); //报价货币
        List<CoinDto> coinDtoList = coinServiceFeign.findCoins(Arrays.asList(sellCoinId, buyCoinId));
        if (CollectionUtils.isEmpty(coinDtoList) || coinDtoList.size() != 2)
            throw new IllegalArgumentException("货币输入错误");
        CoinDto coinDto = coinDtoList.get(0);
        CoinDto coinDto2 = coinDtoList.get(1);
        CoinDto sellDto = null, buyDto = null;
        if (coinDto.getId().equals(sellCoinId)) {
            sellDto = coinDto;
            buyDto = coinDto2;
        }
        else {
            sellDto = coinDto2;
            buyDto = coinDto;
        }
        entity.setName(buyDto.getName()+"/"+sellDto.getName()); //交易市场名称
        entity.setTitle(buyDto.getTitle()+"/"+sellDto.getTitle()); //交易市场标题
        entity.setSymbol(buyDto.getName()+"/"+sellDto.getName()); //交易市场标识
        entity.setImg(sellDto.getImg()); //交易市场图标
        return super.save(entity);
    }

    @Override
    public List<Market> getMarketByTradeAreaId(Long id) {
        return list(new LambdaQueryWrapper<Market>().eq(Market::getTradeAreaId, id)
        .eq(Market::getStatus, 1).orderByAsc(Market::getSort));
    }

    @Override
    public Market getMarketBySymbol(String symbol) {
        return getOne(new LambdaQueryWrapper<Market>().eq(Market::getSymbol, symbol));
    }
}
